<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog" :title="isAdd ? '新增' : '开启拼团'" width="900px">
    <el-form ref="form" :model="form" :rules="rules" :inline="true" size="small" label-width="140px">
      <el-form-item label="拼团名称">
        <el-input v-model="form.title" style="width: 500px;" />
      </el-form-item>
      <el-form-item label="拼团简介">
        <el-input v-model="form.info" style="width: 500px;" />
      </el-form-item>
      <el-form-item label="单位">
        <el-input v-model="form.unitName" style="width: 500px;" />
      </el-form-item>
      <el-form-item label="拼团开始时间">
        <template>
          <el-date-picker
            v-model="form.startTimeDate"
            type="datetime"
            placeholder="选择日期时间"
          />
        </template>
      </el-form-item>
      <el-form-item label="拼团结束时间">
        <template>
          <el-date-picker
            v-model="form.endTimeDate"
            type="datetime"
            placeholder="选择日期时间"
          />
        </template>
      </el-form-item>
      <el-form-item label="产品主图片">
        <MaterialList v-model="form.imageArr" style="width: 500px" type="image" :num="1" :width="150" :height="150" />
      </el-form-item>
      <el-form-item label="产品轮播图">
        <MaterialList v-model="form.sliderImageArr" style="width: 500px" type="image" :num="4" :width="150" :height="150" />
      </el-form-item>
       <!--商品规格属性-->
      <el-form-item  label="规格属性" v-if="form.sta"  >
           <div class="myattr">
                <template v-if="sa">
                      <span :class="attr.check ? 'check':''">属性标签:</span>&nbsp;
                      <el-input v-model="form.items" placeholder="属性标签" style="width: 60%" :number="true" />
                </template>
                <template v-for="(attr,index) in form.attrs" >
                    <el-form-item >
                        <template v-for="(item,index) in attr.detail" >
                          <!-- <el-col :span="3" style="margin-right: 2px  display: block">
                            {{ index }}:{{ item }}
                          </el-col> -->
                          <el-col :span="12" >
                            {{ index }}:{{ item }}
                          </el-col>
                        </template><br/>
                        <el-col :span="8">
                          <span :class="attr.check ? 'check':''">原金额:</span>&nbsp;
                          <el-input v-model="attr.price" placeholder="原金额" style="width: 60%" :number="true"  disabled="disabled"/>
                        </el-col>
                        <el-col :span="8">
                          <span :class="attr.check ? 'check':''">拼团金额:</span>&nbsp;
                          <el-input v-model="attr.pinkPrice"  placeholder="拼团金额" style="width: 60%" :number="true" />
                        </el-col>
                        <el-col :span="8">
                          <span :class="attr.check ? 'check':''">库存:</span>&nbsp;
                          <el-input v-model="attr.stock" placeholder="库存" style="width: 60%" :number="true" />
                        </el-col>
                        <!-- <el-col :span="2" style="margin-right: 3px">
                          <el-button type="primary" @click="removeGoods(index)">删除</el-button>
                        </el-col> -->
                      <!-- </el-row> -->
                    </el-form-item>
                  </template>
            </div>
      </el-form-item>
      
      <el-form-item label="拼团时效(单位小时)">
        <el-input v-model="form.effectiveTime" style="width: 500px;" />
      </el-form-item>
      
      <el-form-item label="拼团人数">
        <el-input v-model="form.people" />
      </el-form-item>
      <el-form-item label="拼团价" v-if="form.sp">
        <el-input v-model="form.price" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="form.sort" />
      </el-form-item>
      <el-form-item label="活动状态">
        <el-radio v-model="form.isShow" :label="1">开启</el-radio>
        <el-radio v-model="form.isShow" :label="0" style="width: 200px;">关闭</el-radio>
      </el-form-item>
      <el-form-item label="详情">
        <editor v-model="form.description" />
      </el-form-item>
    </el-form>
    
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/yxStoreCombination'
import editor from '../../components/Editor'
import MaterialList from '@/components/material'
import {  getAttr } from '@/api/yxStoreProduct'
// import goodattr from '@/views/activity/combination/goodattr'
export default {
  components: { editor,  MaterialList },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      sa :false,
      form: {
        id: '',
        productId: '',
        merId: '',
        image: '',
        images: '',
        imageArr: [],
        sliderImageArr: [],
        title: '',
        attr: '',
        people: '',
        info: '',
        price: '',
        sort: '',
        sales: '',
        stock: '',
        addTime: '',
        isHost: '',
        isShow: '',
        isDel: 0,
        merUse: '',
        isPostage: '',
        postage: '',
        description: '',
        startTime: '',
        stopTime: '',
        effectiveTime: '',
        cost: '',
        browse: '',
        unitName: '',
        combination: 1,
        browse: 0,
        startTimeDate: '',
        endTimeDate: ''
      },
      items: [{
        value: '',
        detailValue: '',
        attrHidden: false,
        detail: []
      }],
      attrs: [],
      sta: false,
      rules: {
      }
    }
  },
  // mounted(){
  //   this.getAttrs(this.form.productId);
  // },
  watch: {
    'form.imageArr': function(val) {
      if (val) {
        this.form.image = val.join(',')
      }
    },
    'form.sliderImageArr': function(val) {
      if (val) {
        this.form.images = val.join(',')
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    //获取属性
    getAttrs() {
        getAttr(this.form.productId).then(res => {
            //res为空
            if (res) {
            this.hidden = true
            this.form.items = res.attr;                         
            this.form.attrs = res.value;
            } else {
            this.hidden = false
            this.items = [{
                value: '',
                detailValue: '',
                attrHidden: false,
                detail: []
            }]
            this.attrs = []
            }
        })
    },
    doEdit() {
      
      let pink = true;
      let ling = true;
      this.form.attrs.forEach(item => {
        if(!item.pinkPrice){
            pink  = false;
        }
        if(item.pinkPrice==0){
            ling  = false;
        }
      });
      if(pink){
          if(ling){
              this.form.attrs=JSON.stringify(this.form.attrs)
              this.form.items=JSON.stringify(this.form.items)
            edit(this.form).then(res => {
              this.resetForm()
              this.$notify({
                title: '操作成功',
                type: 'success',
                duration: 2500
              })
              this.loading = false
              this.$parent.init()
            }).catch(err => {
              this.loading = false;
              this.getAttrs();
              console.log(err.response.data.message)
            })
          }else{
            this.$notify({
              title: '拼团价格必须大于0',
              type: 'error',
              duration: 2500
            })
           this.loading = false
          }
          
      }else{
        this.$notify({
              title: '请输入拼团价格',
              type: 'error',
              duration: 2500
            })
         this.loading = false
      }
     
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        productId: '',
        merId: '',
        image: '',
        images: '',
        imageArr: [],
        sliderImageArr: [],
        title: '',
        attr: '',
        people: 0,
        info: '',
        price: 0,
        sort: '',
        sales: '',
        stock: '',
        addTime: '',
        isHost: '',
        isShow: '',
        isDel: '',
        combination: '',
        merUse: '',
        isPostage: '',
        postage: '',
        description: '',
        startTimeDate: '',
        endTimeDate: '',
        effectiveTime: 24,
        cost: '',
        browse: '',
        unitName: ''
      }
      
    }
  }
}
</script>

<style scoped>
.myattr{
  height: auto;
  width: 70%;
  position: relative;
  top: -20px;
  overflow: hidden;
  /* margin: 10px auto; */
  left: 150px;
 
  border:1px solid  #f0f0f0
}
</style>


